﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Red_Cross_Philippines;
using System.Drawing.Imaging;
using System.IO;

namespace Philippine_National_Red_Cross
{
    public partial class frmRegister : Form
    {
        Blood_Bank_System BloodSystem = new Blood_Bank_System();
        DataTable tbl = new DataTable();
        
        Blood_Client_Donors BloodClient = new Blood_Client_Donors();

        public frmRegister()
        {
            InitializeComponent();
            btnRegister.Click += new EventHandler(btnRegister_Click);
            this.Load += new EventHandler(Form2_Load);
            btnBrowse.Click += new EventHandler(btnBrowse_Click);
        }


        private byte[] ConvertImageToByteArray(Image imageToConvert,ImageFormat formatOfImage)
        {
            byte[] ret;
            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    imageToConvert.Save(ms, formatOfImage);
                    ret = ms.ToArray();
                }
            }
            catch (Exception) { throw; }
            return ret;
        }

        void btnBrowse_Click(object sender, EventArgs e)
        {
            
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
            if (open.ShowDialog() == DialogResult.OK)
            {
                Image img = Image.FromFile(open.FileName);
                byte[] byteimg = ConvertImageToByteArray(img, ImageFormat.Jpeg);
                BloodClient.profilepic = byteimg;
                pictureBox1.ImageLocation = open.FileName;
            }
            
        }

        void btnRegister_Click(object sender, EventArgs e)
        {
           if (txtDate.Text == "")
            {
                MessageBox.Show("Please input the date", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtDate.Focus();
                return;
            }
            else if (cbBloodType.SelectedIndex == -1)
            {
                MessageBox.Show("Please choose your blood type", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                cbBloodType.Focus();
                return;
            }
            else if (txtSurname.Text == "")
            {
                MessageBox.Show("Please input your surname", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtSurname.Focus();
                return;
            }
            else if (txtFirstName.Text =="")
            {
                MessageBox.Show("Please input your first name", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtFirstName.Focus();
                return;
            }
                 else if (txtMiddleName.Text =="")
            {
                MessageBox.Show("Please input your middle name", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtMiddleName.Focus();
                return;
            }
                 else if (txtAge.Text =="")
            {
                MessageBox.Show("Please input your age", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtAge.Focus();
                return;
            }
                 else if (cbSex.SelectedIndex == -1)
            {
                MessageBox.Show("Please choose your gender", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                cbSex.Focus();
                return;
            }
                
                 else if (txtAddress.Text == "")
            {
                MessageBox.Show("Please input your address", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtAddress.Focus();
                return;
            }
                 else if (txtContactNumber.Text == "")
            {
                MessageBox.Show("Please input your contact number", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtContactNumber.Focus();
                return;
            }

            else if (txtContactNumber.Text.Length < 11)
            {
                MessageBox.Show("Invalid Contact Number", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txtContactNumber.Focus();
            }
           else if (pictureBox1.Image == null)
           {
               MessageBox.Show("Please upload your picture first", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
               return;
           }


           else
           {
               ClientSave();
               ClientClear();
               pictureBox1.Image = null;
               MessageBox.Show("Successfully Registered", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
           }
        }



        void Form2_Load(object sender, EventArgs e)
        {

            BloodSystem.Connect();
            BloodSystem.Query("SELECT * FROM tblblooddonor");
            tbl = BloodSystem.tbl;
            BloodSystem.Disconnect();
        }


        private void ClientSave()
        {
            string ClientFullName = txtSurname.Text + "," + txtFirstName.Text + " " + txtMiddleName.Text;
            
            BloodClient.date = txtDate.Text;
            BloodClient.bloodType = cbBloodType.Text;
            BloodClient.Name = ClientFullName;
            BloodClient.Age = txtAge.Text;
            BloodClient.Sex = cbSex.Text;
            BloodClient.birthdate = txtBirthdate.Text;
            BloodClient.address = txtAddress.Text;
            BloodClient.contact = txtContactNumber.Text;
            BloodSystem.Register_Blood_Donors(BloodClient);
        }
        private void ClientClear()
        {
          
            cbBloodType.SelectedIndex = -1;
            txtSurname.Clear();
            txtFirstName.Clear();
            txtMiddleName.Clear();
            txtAge.Clear();
            cbSex.SelectedIndex = -1;
            txtAddress.Clear();
            txtContactNumber.Clear();
        }
    }
}
